<?php
$NRIC = $_REQUEST['NRIC'];
$customerName = $_REQUEST['CustomerName'];
$citizenship = $_REQUEST['Citizenship'];
$beforeDeviation = intval($_REQUEST['BeforeDeviation']);
$afterDeviation = intval($_REQUEST['AfterDeviation']);
$dateOfBirth = $_REQUEST['DateOfBirth'];
$gender = $_REQUEST['Gender'];
$centreId = intval($_REQUEST['CentreName']);

include 'conn.php';

if($centreId == 0){//Centre has not been updated
	$sql = "UPDATE client SET CustomerName='$customerName',Citizenship='$citizenship',BeforeDeviation='$beforeDeviation',AfterDeviation='$afterDeviation', DateOfBirth='$dateOfBirth', Gender='$gender' WHERE NRIC='$NRIC'";
	$result = @mysql_query($sql);

	if ($result)
	{
		echo json_encode(array('success'=>true));
	} else 
	{
		echo json_encode(array('msg'=>"Update Client NOT Successful!"));
		print '<script type="text/javascript">';
		print 'alert("Update Client NOT Successful!")';
		print '</script>';
	}
}
else if($centreId == -1){//Centre has been removed from the client's table
	$sql = "UPDATE client SET CustomerName='$customerName',Citizenship='$citizenship',BeforeDeviation='$beforeDeviation',AfterDeviation='$afterDeviation', DateOfBirth='$dateOfBirth', Gender='$gender', CentreId=null WHERE NRIC='$NRIC'";
	$result = @mysql_query($sql);

	if ($result)
	{
		echo json_encode(array('success'=>true));
	} else 
	{
		echo json_encode(array('msg'=>"Update Client NOT Successful!"));
		print '<script type="text/javascript">';
		print 'alert("Update Client NOT Successful!")';
		print '</script>';
	}
}
else{//Centre has been updated
	$sql = "UPDATE client SET CustomerName='$customerName',Citizenship='$citizenship',BeforeDeviation='$beforeDeviation',AfterDeviation='$afterDeviation', DateOfBirth='$dateOfBirth', Gender='$gender', CentreId='$centreId' WHERE NRIC='$NRIC'";
	$result = @mysql_query($sql);

	if ($result)
	{
		echo json_encode(array('success'=>true));
	} else 
	{
		echo json_encode(array('msg'=>"Update Client NOT Successful!"));
		print '<script type="text/javascript">';
		print 'alert("Update Client NOT Successful!")';
		print '</script>';
	}
}
?>